package com.example.myapplication.tack_08;

import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.viewpager.widget.ViewPager;

import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;

import com.example.myapplication.R;
import com.example.myapplication.tack_08.adapter.HdListAdpater;
import com.example.myapplication.tack_08.data.BannerData;
import com.example.myapplication.tack_08.data.HdListData;
import com.example.myapplication.tack_08.data.HdflData;
import com.example.myapplication.utils.BannerTool;
import com.example.myapplication.utils.OkHttpTool;
import com.google.android.material.tabs.TabLayout;

import java.util.ArrayList;
import java.util.List;

public class Activity extends AppCompatActivity {
    private ImageView exit;
    private List<BannerData.RowsDTO> bannerList;
    private ViewPager banner;
    private TabLayout tabLayout;
    private List<HdflData.DataDTO> listHdfl;
    private List<HdListData.RowsDTO> hdListDataList;
    private RecyclerView list;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.tack_8_activity);
        init();
        exit_();
        banner_();
        hdfl();
    }

    private void hdList(String id) {
        String url ="prod-api/api/activity/activity/list?categoryId="+id;
        OkHttpTool.doNetRequest(url, OkHttpTool.GET, HdListData.class, new OkHttpTool.NetCallback() {
            @Override
            public void onSuccess(Object obj) {

                HdListData hdflData = (HdListData) obj;
                if(hdflData.getCode() == 200){
                    hdListDataList = hdflData.getRows();
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            hdListAdpater();
                        }
                    });
                }

            }

            @Override
            public void onFailure(String msg) {

            }
        });
    }
    private void hdListAdpater() {
        HdListAdpater hdListAdpater= new HdListAdpater(hdListDataList,Activity.this);
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(Activity.this);
        list.setLayoutManager(linearLayoutManager);
        list.setAdapter(hdListAdpater);
    }
    private void banner_() {
        String url = "prod-api/api/activity/rotation/list";
        OkHttpTool.doNetRequest(url, OkHttpTool.GET, BannerData.class, new OkHttpTool.NetCallback() {
            @Override
            public void onSuccess(Object obj) {
                BannerData bannerData = (BannerData)obj;
                bannerList = bannerData.getRows();
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        ArrayList<String> list = new ArrayList<>();
                        for (BannerData.RowsDTO rowsDTO : bannerList) {
                            list.add(rowsDTO.getAdvImg());
                        }
                        BannerTool bannerTool = new BannerTool(Activity.this,list,banner);
                        banner.setAdapter(bannerTool);
                    }
                });
            }

            @Override
            public void onFailure(String msg) {

            }
        });
    }
    private void hdfl() {
        String url = "prod-api/api/activity/category/list";
        OkHttpTool.doNetRequest(url, OkHttpTool.GET, HdflData.class, new OkHttpTool.NetCallback() {
            @Override
            public void onSuccess(Object obj) {
                HdflData hdflData =(HdflData) obj;
                listHdfl = hdflData.getData();
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        for (HdflData.DataDTO dataDTO : listHdfl) {
                            TabLayout.Tab tab = tabLayout.newTab();
                            tab.setText(dataDTO.getName());
                            tabLayout.addTab(tab);
                        }
                        hdflAdpater();
                        hdList(String.valueOf(listHdfl.get(0).getId()));
                    }
                });
            }
            @Override
            public void onFailure(String msg) {

            }
        });
    }

    private void hdflAdpater() {
        tabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                int position = tab.getPosition();
                String id = String.valueOf(listHdfl.get(position).getId());
                hdList(id);
            }

            @Override
            public void onTabUnselected(TabLayout.Tab tab) {

            }

            @Override
            public void onTabReselected(TabLayout.Tab tab) {

            }
        });
    }


    private void exit_() {
        exit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Activity.this.finish();
            }
        });
    }

    private void init() {
        exit = findViewById(R.id.exit);
        banner = findViewById(R.id.banner);
        tabLayout = findViewById(R.id.hdfl);
        list = findViewById(R.id.list);
    }
}